package iit.projects.etaxi.view.bean;

import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;

import iit.projects.etaxi.model.entity.Customer;
import iit.projects.etaxi.model.entity.Operator;
import iit.projects.etaxi.model.service.CustomerService;
import iit.projects.etaxi.model.service.OperatorService;
import iit.projects.etaxi.model.service.ServiceLocator;
import iit.projects.etaxi.util.Constants;
import iit.projects.etaxi.util.JSFUtils;

import com.icesoft.faces.component.ext.HtmlInputSecret;

public class ChangePasswordBackingBean {
	
	private String oldPassword;
	private String newPassword;
	private String retypeNewPassword;
	
	private HtmlInputSecret compOldPwd;
	private HtmlInputSecret compNewPwd;
	private HtmlInputSecret compRetypeNewPwd;
	
	private ServiceLocator serviceLocator;
	private CustomerService customerService;
	private OperatorService operatorService;
	
	@javax.annotation.PostConstruct
	public void init(){
		customerService = serviceLocator.getCustomerService();
		operatorService = serviceLocator.getOperatorService();
	}
	
	public void save(ActionEvent event){
		//boolean loggedAsCustomer = (Boolean)JSFUtils.getFromSession(Constants.LOGGED_IN_AS_CUSTOMER);
		
		if(JSFUtils.getFromSession(Constants.LOGGED_IN_AS_CUSTOMER) != null){
			Customer customer = (Customer)JSFUtils.getFromSession(Constants.CUSTOMER);
			if(!valid(customer.getPassword())){
				return;
			}
			else{
				customer.setPassword(newPassword);
				try{
					Customer updatedCustomer = customerService.updateCustomer(customer);
					JSFUtils.storeOnSession(Constants.CUSTOMER, updatedCustomer);
					JSFUtils.addFacesInfoMessage("passsword.change.successfull");
				}catch(Throwable e){
					return;
				}
			}
		}
		else{
			Operator operator = (Operator)JSFUtils.getFromSession(Constants.OPERATOR);
			if(!valid(operator.getPassword())){
				return;
			}
			else{
				operator.setPassword(newPassword);
				try{
					Operator updatedOperator = operatorService.updateOperator(operator);
					JSFUtils.storeOnSession(Constants.OPERATOR, updatedOperator);
					JSFUtils.addFacesInfoMessage("passsword.change.successfull");
				}catch(Throwable e){
					return;
				}
			}
		}
	}
	
	private boolean valid(String oldPassword){
		boolean valid = true;
		
		if(this.oldPassword == null || this.oldPassword.isEmpty()){
			JSFUtils.addFacesErrorMessage(compOldPwd, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		if(newPassword == null || newPassword.isEmpty()){
			JSFUtils.addFacesErrorMessage(compNewPwd, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		if(retypeNewPassword == null || retypeNewPassword.isEmpty()){
			JSFUtils.addFacesErrorMessage(compRetypeNewPwd, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		if(!valid){
			return valid;
		}
		
		if(!oldPassword.equals(this.oldPassword)){
			JSFUtils.addFacesErrorMessage(compOldPwd, "incorrect.password");
			return false;
		}
		if(!newPassword.equals(retypeNewPassword)){
			JSFUtils.addFacesErrorMessage(compRetypeNewPwd, "passwords.doesnotmatch");
			return false;
		}
		
		return valid;
		
	}
	
	/*private void clear(){
		oldPassword = null;
		newPassword = null;
		retypeNewPassword = null;
		
		compOldPwd.resetValue();
		compNewPwd.resetValue();
		compRetypeNewPwd.resetValue();
	}*/
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getRetypeNewPassword() {
		return retypeNewPassword;
	}
	public void setRetypeNewPassword(String retypeNewPassword) {
		this.retypeNewPassword = retypeNewPassword;
	}
	public HtmlInputSecret getCompOldPwd() {
		return compOldPwd;
	}
	public void setCompOldPwd(HtmlInputSecret compOldPwd) {
		this.compOldPwd = compOldPwd;
	}
	public HtmlInputSecret getCompNewPwd() {
		return compNewPwd;
	}
	public void setCompNewPwd(HtmlInputSecret compNewPwd) {
		this.compNewPwd = compNewPwd;
	}
	public HtmlInputSecret getCompRetypeNewPwd() {
		return compRetypeNewPwd;
	}
	public void setCompRetypeNewPwd(HtmlInputSecret compRetypeNewPwd) {
		this.compRetypeNewPwd = compRetypeNewPwd;
	}

	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}
	

}
